﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class QuarterPlan_AddYearIntroduction : System.Web.UI.Page
{
    Model.UserInfo objUserInfo = new Model.UserInfo();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDrp();
        }

    }

    /// <summary>
    /// 绑定年计划前言
    /// </summary>
    void BindGdv()
    {
        object[] _value = new object[3];
        _value[0] = this.drpYear.SelectedValue;
        _value[1] = Convert.ToInt32(this.drpDepartment.SelectedValue);
        _value[2] = 0;
        DataTable dt = new BLL.YearIntroduction().GetYearIntroductionByDepartmentAndYear(_value);
        this.gdvYearIntroduction.DataSource = dt;
        this.gdvYearIntroduction.DataBind();
        for (int i = 0; i < this.gdvYearIntroduction.Rows.Count; i++)
        {
            string UploadUrl = this.gdvYearIntroduction.DataKeys[i]["UploadUrl"].ToString();
            //Button btn = (Button)this.gdvYearIntroduction.Rows[i].FindControl("btnView");
            //if (UploadUrl.Length > 0)
            //{
            //    btn.Visible = true;
            //}
            //else
            //{
            //    btn.Visible = false;
            //}
        }
        this.txtSortId.Text = (this.gdvYearIntroduction.Rows.Count + 1).ToString();

    }


    /// <summary>
    /// 绑定下拉信息
    /// </summary>
    void BindDrp()
    {
        for (int i = 2009; i < 2100; i++)
        {
            ListItem li = new ListItem(i.ToString(), i.ToString());
            this.drpYear.Items.Add(li);

        }
        DataTable _year = new BLL.QuarterDateManager().getQuarterDate();
        this.drpYear.SelectedIndex = this.drpYear.Items.IndexOf(this.drpYear.Items.FindByText(_year.Rows[0][2].ToString()));

        if (objUserInfo.UserName != "admin")
        {
            object[] _value = new object[1];
            _value[0] = objUserInfo.UserName;
            DataTable _userTable = new BLL.UserInfoManager().get(BLL.UserInfoManager.field.UserName.ToString(), _value);
            if (_userTable.Rows.Count > 0)
            {
                if (_userTable.Rows[0]["DepartmentId"].ToString().Length > 0)
                {
                    ListItem _li = new ListItem(_userTable.Rows[0]["DepartmentId"].ToString(), _userTable.Rows[0]["DId"].ToString());
                    this.drpDepartment.Items.Add(_li);
                }
                if (_userTable.Rows[0]["PartTimeDepartmentId"].ToString().Length > 0)
                {
                    ListItem _li = new ListItem(_userTable.Rows[0]["PartTimeDepartmentId"].ToString(), _userTable.Rows[0]["PDid"].ToString());
                    this.drpDepartment.Items.Add(_li);
                }
            }
            this.lblDepartment.Text = this.drpDepartment.SelectedItem.Text;
        }
        else
        {
            this.drpDepartment.DataSource = new BLL.DepartmentInfoManager().getNoTree();
            this.drpDepartment.DataTextField = "DepartmentName";
            this.drpDepartment.DataValueField = "DepartmentId";
            this.drpDepartment.DataBind();
            ListItem _li = new ListItem("请选择部门", "0");
            this.drpDepartment.Items.Insert(0, _li);
            this.lblDepartment.Text = this.drpDepartment.SelectedItem.Text;
        }
        this.drpDepartment.SelectedValue = Utility.HttpHelp.getParamsString("Department");
        BindGdv();
    }

    /// <summary>
    /// 查看年计划前言
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnView_Click(object sender, EventArgs e)
    {
        //得到button所在行        
        Button bt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)bt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        int _rowIndex = iRow.RowIndex;//得到该行的索引

        string Id = this.gdvYearIntroduction.DataKeys[_rowIndex]["Id"].ToString();
        string _url = "DisplayAttachment.aspx?Id=" + Id + "&TypeId=1";
        Page.RegisterStartupScript("windowsopen", "<script language=javascript>window.open('" + _url + "','quarterplandetailmodify','height=800,width=1000,top=300,left=250,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,status=no');</script>");
    }

    /// <summary>
    /// 添加方法
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (this.drpDepartment.SelectedValue != "0")
        {
            object[] _value = new object[8];
            _value[0] = Convert.ToInt32(this.txtSortId.Text);
            _value[1] = this.txtYearIntroduction.Text;
            _value[2] = DateTime.Now;
            object[] _valueUser = new object[1];
            _valueUser[0] = objUserInfo.UserName;
            DataTable dt = new BLL.UserInfoManager().get(BLL.UserInfoManager.field.UserName.ToString(), _valueUser);
            if (dt.Rows.Count > 0)
            {
                _value[3] = dt.Rows[0]["RealName"].ToString();
                _value[4] = Convert.ToInt32(dt.Rows[0]["UserId"]);
            }
            _value[5] = this.drpYear.SelectedItem.Text;
            _value[6] = this.drpDepartment.SelectedItem.Text;
            _value[7] = Convert.ToInt32(this.drpDepartment.SelectedValue);
            int val = new BLL.YearIntroduction().Add(_value);
            if (val > 0)
            {
                this.txtSortId.Text = string.Empty;
                this.txtYearIntroduction.Text = string.Empty;
            }
            else
            {
                Utility.HttpHelp.alert("添加失败", this.Page);
            }
            BindGdv();
        }
        else
        {
            Utility.HttpHelp.alert("请选择部门", this.Page);
        }
    }



    /// <summary>
    /// 上传文件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAttachmentPath_Click(object sender, EventArgs e)
    {
        string Id = "";
        int _count = 0;
        for (int i = 0; i < this.gdvYearIntroduction.Rows.Count; i++)
        {
            CheckBox ckb = (CheckBox)this.gdvYearIntroduction.Rows[i].Cells[0].FindControl("ckbSelect");
            if (ckb.Checked == true)
            {
                Id = this.gdvYearIntroduction.DataKeys[i]["Id"].ToString();
                _count++;
            }
        }
        if (_count == 1)
        {
            //定向到添加年附件路径页面
            string _url = "../WebCommon/AttachmentPath.aspx?PlanId=" + Id + "&PlanType=19&Department="+this.drpDepartment.SelectedValue;
            Page.RegisterStartupScript("windowsopen", "<script language=javascript>window.open('" + _url + "','quarterplandetailmodify','height=40,width=400,top=300,left=300,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no');</script>");
        }
        if (_count > 1)
        {
            Utility.HttpHelp.alert("只能选择一个", Page);
            return;
        }
        if (_count == 0)
        {
            Utility.HttpHelp.alert("请选择一个", Page);
            return;
        }
    }

    protected void drpDepartment_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGdv();
        this.lblDepartment.Text = this.drpDepartment.SelectedItem.Text;
    }
    protected void drpYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGdv();
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDel_Click(object sender, EventArgs e)
    {
        //得到button所在行        
        Button bt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)bt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        int _rowIndex = iRow.RowIndex;//得到该行的索引

        int Id = Convert.ToInt32(this.gdvYearIntroduction.DataKeys[_rowIndex]["Id"]);
        object[] _value = new object[1];
        _value[0] = Id;
        int val = new BLL.YearIntroduction().DelYearIntroductionById(_value);
        if (val > 0)
        {
            Utility.HttpHelp.alert("删除成功", this.Page);
        }
        else
        {
            Utility.HttpHelp.alert("删除失败", this.Page);
        }
        BindGdv();
    }


    /// <summary>
    /// 提交
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < this.gdvYearIntroduction.Rows.Count; i++)
        {
            CheckBox ckb = (CheckBox)this.gdvYearIntroduction.Rows[i].FindControl("ckbSelect");
            if (ckb.Checked)
            {
                int Id = Convert.ToInt32(this.gdvYearIntroduction.DataKeys[i]["Id"]);
                object[] _value = new object[1];
                _value[0] = Id;
                int val = new BLL.YearIntroduction().SubmitYearIntroduction(_value);
                if (val > 0)
                {

                    Utility.HttpHelp.alert("添加成功", this.Page);
                }
            }
        }
    }
}